<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/14/0014
 * Time: 11:09
 */

namespace app\sports_admin\controller;


use think\Request;
use app\sports_admin\model\Course as CourseModel;
use app\sports_admin\model\CheckOut as CheckOutModel;
use app\sports_admin\model\Sport as SportModel;
use app\sports_admin\model\Test as TestModel;
class Course extends Base
{
    protected $beforeActionList = [
        'isData'=>['only'=>'index,course,classrecord'],
        'operationSelect'=>['only'=>'index,course,getcourseinfo,classrecord,getclassrecordinfo'],
    ];
    /*
     * 教学数据首页
     */
    public function index()
    {
        return view('index');
    }
    /*
     * 排课首页
     */
    public function course()
    {
        return view('course');
    }
    /**
     * 获取排课信息
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     * @param $courseName 课程名称
     * @param $teacherName 教师名称
     * @param $schoolName 学校名称
     * @param $year 要检索的学年
     */
    public function getCourseInfo()
    {
        $params = Request::instance()->only(['page','rows','sort','sortOrder','courseName','teacherName','schoolName','year']);
        $page = intval($params['page']) ?  intval($params['page']) : 1;
        $rows = intval($params['rows']) ?  intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        $courseName = trim($params['courseName']);
        $teacherName = trim($params['teacherName']);
        $schoolName = trim($params['schoolName']);
        $year = trim($params['year']);
        $info = CourseModel::getCourseInfo($page,$rows,$sort,$sortOrder,$courseName,$teacherName,$schoolName,$year);
        if(!empty($teacherName) || !empty($schoolName)){
            $params = [];
            $val = [];
            foreach($info['rows'] as $value){
                if(isset($value->get_teacher_info->teacher_name) && isset($value->get_teacher_info->school->school_name)){
                    $val['id'] = $value->id;
                    $val['year'] = $value->year;
                    $val['monday'] = $value->monday;
                    $val['tuesday'] = $value->tuesday;
                    $val['wednesday'] = $value->wednesday;
                    $val['thursday'] = $value->thursday;
                    $val['friday'] = $value->friday;
                    $val['mark'] = $value->mark;
                    $val['course_name'] = $value->course_name;
                    $val['get_teacher_info']['teacher_name'] = $value->get_teacher_info->teacher_name;
                    $val['get_teacher_info']['school']['school_name'] = $value->get_teacher_info->school->school_name;
                    $val['get_teacher_info']['school']['school_id'] = $value->get_teacher_info->school->school_id;
                    $params[] = $val;
                }
            }
            return json(['rows'=>$params,'total'=>count($params)]);
        }else{
            return json($info);
        }
    }
    /*
     * 上课记录页面
     * @param $teacherId 教师id
     */
    public function classRecord()
    {
        $teacherId = input('get.teacher_id');
        $project = SportModel::field('id,sport_name')->select();
        $testInfo = TestModel::field('id,test_name')->select();
        return view('record',compact('teacherId','project','testInfo'));
    }
    /*
     * 获取上课记录信息
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     * @param $teacherId 教师id
     * @param $sportId 类型名称id
     * @param $testId 测试名称id
     */
    public function getClassRecordInfo()
    {
        $params = Request::instance()->only(['page','rows','sort','sortOrder','teacherId','sportId','testId']);
        $page = intval($params['page']) ?  intval($params['page']) : 1;
        $rows = intval($params['rows']) ?  intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        $teacherId = trim($params['teacherId']);
        $sportId = intval($params['sportId']);
        $testId = intval($params['testId']);
        return CheckOutModel::getClassRecordInfo($page,$rows,$sort,$sortOrder,$teacherId,$sportId,$testId);
    }
}